package com.xgg.taiwuinfo.filter.fc;

import android.util.Log;

import com.xgg.taiwuinfo.filter.FC_SelectCompareNum;
import com.xgg.taiwuinfo.filter.FilterComponent;
import com.xgg.taiwuinfo.gongfa.abilities.Ability_Cast;
import com.xgg.taiwuinfo.gongfa.base.Gongfa;
import com.xgg.taiwuinfo.gongfa.base.Gongfa_Cuipo;
import com.xgg.taiwuinfo.gongfa.base.Gongfa_Jueji_Huti;
import com.xgg.taiwuinfo.utils.GongfaType;

import org.json.JSONObject;

import java.util.ArrayList;

public class FCCastCompareNum extends FC_SelectCompareNum {

    public FCCastCompareNum(JSONObject obj)
    {
        mTypeId = FilterComponent.FT_CAST_COMAPRE_NUM;
        if (obj != null) {
            try {
                ReadField = obj.getInt("f");
                ReadCompare = obj.getInt("c");
                ReadValue = obj.getString("v");
            } catch (Exception e) {
                Log.d("LoadJson", e.getLocalizedMessage());
                ReadField = 0;
                ReadCompare = FC_GREATER;
                ReadValue = "0";
            }
        }
        else {
            ReadField = 0;
            ReadCompare = FC_GREATER;
            ReadValue = "0";
        }
        TempField = ReadField;
        TempValue = ReadValue;
        TempCompare = ReadCompare;

        if (mFieldMap == null) {
            mFieldMap = new ArrayList<>();
            AddMap(mFieldMap, "运功时间", "0");
            AddMap(mFieldMap, "架势消耗(%)", "1");
            AddMap(mFieldMap, "提气消耗(%)", "2");
        }
    }

    @Override
    public String FilterGongfa(Gongfa gongfa)
    {
        int type = gongfa.getType();
        Ability_Cast cast = null;
        if (type == GongfaType.TYPE_JUEJI_HUTI){
            cast = ((Gongfa_Jueji_Huti)gongfa).ability_cast;
        }
        else if ((type & GongfaType.TYPE_CUIPO) > 0) {
            cast = ((Gongfa_Cuipo)gongfa).ability_cast;
        }
        else
            return null;
        double editvalue = Double.parseDouble(ReadValue);
        String rt;
        int gfvalue = 0;        //功法内数据
        int edvalue = 0;        //用户填写的数据
        switch (ReadField)
        {
            case 0:
                gfvalue = cast.getCastTime();
                rt = String.format("施法时间: %.01f" , (double)(cast.getCastTime()) / 10.0);
                edvalue = (int)(editvalue * 10);
                break;
            case 1:
                gfvalue = cast.getJSCost();
                rt = String.format("架势消耗: %d %%" , cast.getJSCost());
                edvalue = (int)editvalue;
                break;
            case 2:
                gfvalue = cast.getTQCost();
                rt = String.format("提气消耗: %d %%" , cast.getTQCost());
                edvalue = (int)editvalue;
                break;
            default:
                return null;
        }
        switch (ReadCompare)
        {
            case FC_EQUAL:
                if (gfvalue == edvalue) {
                    return rt;
                }
                else{
                    return null;
                }
            case FC_GREATER:
                if (gfvalue > edvalue) {
                    return rt;
                }
                else{
                    return null;
                }
            case FC_GREATERCONTAIN:
                if (gfvalue >= edvalue) {
                    return rt;
                }
                else {
                    return null;
                }
            case FC_SMALLER:
                if (gfvalue < edvalue) {
                    return rt;
                }
                else{
                    return null;
                }
            case FC_SMALLERCONTAIN:
                if (gfvalue <= edvalue) {
                    return rt;
                }
                else {
                    return null;
                }
            default:
                return null;
        }
    }
}
